# Maintainer: Jeroen Ooms <jeroen@berkeley.edu>

_realname=libsbml
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.20.4
pkgrel=2
pkgdesc="A file format for exchanging computational models in systems biology (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://sbml.org/software/libsbml/'
msys2_repository_url="https://github.com/sbmlteam/libsbml"
license=("LGPL-2.1-or-later")
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-swig")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python bindings")
source=("https://github.com/sbmlteam/libsbml/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        02-install-layout.patch)
sha256sums=('02c225d3513e1f5d6e3c0168456f568e67f006eddaab82f09b4bdf0d53d2050e'
            '545cb5460c6e2af6c1b790719435c2b54a47574b226c10aa6c0914242503d217')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}"/02-install-layout.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DWITH_SWIG=OFF \
    -DWITH_CPP_NAMESPACE=ON \
    -DLIBSBML_DEPENDENCY_DIR=${MINGW_PREFIX} \
    -DLIBXML_INCLUDE_DIR="${MINGW_PREFIX}/include/libxml2" \
    -DWITH_LIBXML=ON \
    -DWITH_EXPAT=OFF \
    -DWITH_BZIP2=ON \
    -DWITH_ZLIB=ON \
    -DWITH_PYTHON=ON \
    -DENABLE_LAYOUT=ON \
    -DENABLE_RENDER=ON \
    -DENABLE_QUAL=ON \
    -DENABLE_MULTI=ON \
    -DENABLE_COMP=ON \
    -DENABLE_FBC=ON \
    -DENABLE_GROUPS=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})

  # Remove path reference in pkgconfig file
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

  # Remove path reference in cmake files
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
